package indi.xliuovo.coursedesign.musicplayer;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ForgetPwdFragment#newInstance} factory method to
 * create an instance of this fragment.
 * @author yangLiu
 */
public class ForgetPwdFragment extends Fragment {
    View forgetPwdFragment;
    EditText  userTel,setNewPwd,answer_1,answer_2;
    Button btnSetNewPwd;
    List<String> questionList = new ArrayList<String>();
    Spinner question_1, question_2;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private String  theResult;

    public ForgetPwdFragment() {}

    public static ForgetPwdFragment newInstance(String param1, String param2) {
        ForgetPwdFragment fragment = new ForgetPwdFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        // 添加密保问题
        questionList.add("您目前的姓名是？");
        questionList.add("您的生日是？");
        questionList.add("您的学号（或工号）是？");
        questionList.add("您母亲的生日是？");
        questionList.add("您配偶的姓名是？");
        questionList.add("对您影响最大的人名字是？");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        forgetPwdFragment = inflater.inflate(R.layout.fragment_forget_pwd, container, false);
        return forgetPwdFragment;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initWidget();
        btnSetNewPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isEmpty()) {
                    if (isFull()) {
                        // 传递数据
                        Map<String, String> loginUser = new HashMap<String, String>();
                        loginUser.put("userId", userTel.getText().toString());
                        loginUser.put("newPwd", setNewPwd.getText().toString());
                        loginUser.put("answer1", answer_1.getText().toString());
                        loginUser.put("answer2", answer_2.getText().toString());
                        MyThread myThread = new MyThread();
                        myThread.toParams("user/resetPwd", loginUser); // 处理数据
                        myThread.start();
                        try {
                            Thread.sleep(500); // 等待返回的结果
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        theResult = myThread.result.get("msg").toString();
                        Toast.makeText(getContext(), theResult, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

    }
    void initWidget() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_selectable_list_item, questionList);
        question_1 = forgetPwdFragment.findViewById(R.id.question_1);
        question_1.setAdapter(adapter);
        question_2 = forgetPwdFragment.findViewById(R.id.question_2);
        question_2.setAdapter(adapter);
        answer_1 = forgetPwdFragment.findViewById(R.id.Answer_1);
        answer_2 = forgetPwdFragment.findViewById(R.id.Answer_2);
        btnSetNewPwd = forgetPwdFragment.findViewById(R.id.setNewPwd);
        userTel = forgetPwdFragment.findViewById(R.id.userTel);
        setNewPwd = forgetPwdFragment.findViewById(R.id.newPwd);

    }
    public boolean isFull() {
        if (userTel.getText().length() == 11)
            return true;
        else {
            Toast.makeText(getContext(), "手机号不足11位", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
    boolean isEmpty() {
        if (TextUtils.isEmpty(userTel.getText()) || TextUtils.isEmpty(setNewPwd.getText().toString()) || TextUtils.isEmpty(answer_1.getText().toString()) || TextUtils.isEmpty(answer_2.getText().toString()) ) {
            Toast.makeText(getContext(), "有输入项为空", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}